using System;
using System.Web;
using digit.Core;
using SMS.SmManager;
using UserManager;

namespace SMS.UserManager
{
    public class ClientState
    {
        static ClientState() { }

        public static bool IsAuthenticated
        {
            get { return MemberState.IsAuthenticated; }
        }

        public static IMember MemberInfo
        {
            get { return MemberState.Member; }
        }

        public static bool ChangePass(string username, string oldpass, string newpass)
        {
            if (string.IsNullOrEmpty(username) || string.IsNullOrEmpty(newpass)) return false;

            bool result = MembersManager.ChangePass(username, oldpass, newpass);

            if (result) return true;
            else
                return false;
        }

        public static bool Login(string username, string password)
        {
            if (string.IsNullOrEmpty(username) || string.IsNullOrEmpty(password)) return false;

            MembersInfo mi = MembersManager.Select(username);
            string p1 = Encryption.MD5Hash(username + password);
            string p2 = Encryption.MD5Encrypt(password);

            if (mi == null || (mi.Password != p1 && mi.Password != p2)) return false;

            UserInfo ms = new UserInfo(mi);

            HttpContext.Current.Session["Member"] = ms;
            return true;
        }

        public static void Logout()
        {
            if (!ClientState.IsAuthenticated) return;

            HttpContext.Current.Session.Clear();
            HttpContext.Current.Session.Abandon();
        }
    }
}